package com.lxs.mall.service.Impl;

import com.lxs.mall.common.Constant;
import com.lxs.mall.exception.LxsMallException;
import com.lxs.mall.exception.LxsMallExceptionEnum;
import com.lxs.mall.model.dao.CartMapper;
import com.lxs.mall.model.dao.ProductMapper;
import com.lxs.mall.model.pojo.Cart;
import com.lxs.mall.model.pojo.Product;
import com.lxs.mall.model.vo.CartVo;
import com.lxs.mall.service.CartService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 小松
 */
@Service
public class CartServiceImpl implements CartService {
    @Resource
    CartMapper cartMapper;
    @Resource
    ProductMapper productMapper;

    @Override
    public List<CartVo> list(Integer userId) {
        List<CartVo> cartVoList = cartMapper.selectList(userId);
        for (CartVo cartVO : cartVoList) {
            cartVO.setTotalPrice(cartVO.getQuantity() * cartVO.getPrice());
        }
        return cartVoList;
    }

    @Override
    public List<CartVo> add(Integer userId, Integer productId, Integer count) {
        validProduct(productId, count);
        Cart cart = cartMapper.selectByUserIdAndProductId(userId, productId);
        if (cart == null) {
            //cart==null表示购物车没有这个商品执行添加商品
            cart = new Cart();
            cart.setProductId(productId);
            cart.setUserId(userId);
            cart.setQuantity(count);
            cart.setSelected(Constant.CartSelected.CHECKED);
            cartMapper.insertSelective(cart);
        } else {
            //cart!=null表示购物车有这个商品执行更新商品
            Cart newCart = new Cart();
            count = cart.getQuantity() + count;
            newCart.setId(cart.getId());
            newCart.setProductId(productId);
            newCart.setUserId(userId);
            newCart.setQuantity(count);
            newCart.setSelected(Constant.CartSelected.CHECKED);
            cartMapper.updateByPrimaryKeySelective(newCart);
        }
        return this.list(userId);
    }

    public void validProduct(Integer productId, Integer count) {
        Product product = productMapper.selectByPrimaryKey(productId);
        if (product == null || product.getStatus().equals(Constant.SellStatus.NOT_SELL)) {
            throw new LxsMallException(LxsMallExceptionEnum.NOT_SELL);
        }
        if (count > product.getStock()) {
            throw new LxsMallException(LxsMallExceptionEnum.NOT_ENOUGH);
        }
    }

    @Override
    public List<CartVo> update(Integer userId, Integer productId, Integer count) {
        validProduct(productId, count);
        Cart cart = cartMapper.selectByUserIdAndProductId(userId, productId);
        if (cart == null) {
            //这个商品不在购物车，无法更新
            throw new LxsMallException(LxsMallExceptionEnum.UPDATE_FAILED);
        } else {
            //cart!=null表示购物车有这个商品执行更新商品的数量
            Cart newCart = new Cart();
            newCart.setId(cart.getId());
            newCart.setProductId(productId);
            newCart.setUserId(userId);
            newCart.setQuantity(count);
            newCart.setSelected(Constant.CartSelected.CHECKED);
            cartMapper.updateByPrimaryKeySelective(newCart);
        }
        return this.list(userId);
    }

    @Override
    public List<CartVo> delete(Integer userId, Integer productId) {
        Cart cart = cartMapper.selectByUserIdAndProductId(userId, productId);
        if (cart == null) {
            //这个商品不在购物车，无法删除
            throw new LxsMallException(LxsMallExceptionEnum.DELETE_FAILED);
        } else {
            //cart!=null表示购物车有这个商品执行删除商品
            cartMapper.deleteByPrimaryKey(cart.getId());
        }
        return this.list(userId);
    }

    @Override
    public List<CartVo> select(Integer userId, Integer productId, Integer selected) {
        Cart cart = cartMapper.selectByUserIdAndProductId(userId, productId);
        if (cart == null) {
            //这个商品不在购物车，无法更新
            throw new LxsMallException(LxsMallExceptionEnum.UPDATE_FAILED);
        } else {
            //cart!=null表示购物车有这个商品执行
            cartMapper.selectOrNot(userId, productId, selected);
        }
        return this.list(userId);
    }

    @Override
    public List<CartVo> selectAll(Integer userId, Integer selected) {
        //改变选中状态
        cartMapper.selectOrNot(userId, null, selected);
        return this.list(userId);
    }
}
